//
//  SISSExcelContentTableView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/8/6.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSExcelContentTableView.h"
#import "SISSExcelView.h"
#import "SISSExcelContentTableViewCell.h"
#import "SISSExcelContentCollectionViewCell.h"

NSString *const tableViewCellIdentifier_Excel = @"ContentTableViewCell";
NSString *const collectionViewCellIdentifier_Excel = @"CollectionViewCell";
@interface SISSExcelContentTableView ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@end
@implementation SISSExcelContentTableView {
    CGFloat superWidth;
    CGFloat superHeight;
}

- (void)dealloc {
    self.delegate = nil;
    self.dataSource = nil;
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.alwaysBounceHorizontal = NO;
        [self registerClass:[SISSExcelContentTableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier_Excel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (superWidth) { //当屏幕旋转的时候去刷新 便宜量
        if (superWidth != self.bounds.size.width) {
            superWidth = self.bounds.size.width;
            SISSExcelContentTableViewCell *coll = [self.visibleCells firstObject];
            for (SISSExcelContentTableViewCell *cell in self.visibleCells) {
                cell.contentCollectionView.contentOffset = coll.contentCollectionView.contentOffset;
            }
        }
    }
    else {
        superWidth = self.bounds.size.width;
        superHeight = self.bounds.size.height;
    }
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    if (scrollView == self) {
        
        //通知lefTableView滑动
        [self setValue:[NSValue valueWithCGPoint:scrollView.contentOffset] forKey:Excel_contentTableViewContentOffset];
    }
    if ([scrollView isKindOfClass:[UICollectionView class]]) {
        if (scrollView.contentOffset.y != 0) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
            return;
        }
        @synchronized(self) {
            [self setValue:[NSValue valueWithCGPoint:scrollView.contentOffset] forKey:Excel_collectionViewContentOffset];
            for (SISSExcelContentTableViewCell *cell in self.visibleCells) {
                cell.contentCollectionView.contentOffset = scrollView.contentOffset;
            }
        }
    }
}
#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource>
// 使用left 来解决这个问题
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([self.excelView.dataSource respondsToSelector:@selector(numberOfColumnInExcelView:numberOfRowsInSection:)]) {
        return [self.excelView.dataSource numberOfColumnInExcelView:self.excelView numberOfRowsInSection:collectionView.tag];
    }
    return 100;
}

#pragma mark 设置 ContentTableViewCell 选中状态
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //列号    indexPath.row
    NSIndexPath *contentIndexPath = [self getIndexPathWith:collectionView];
    //contentIndexPath  区 、 行    。
    SISSExcelContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellIdentifier_Excel forIndexPath:indexPath];
   
    if ([self.excelView.dataSource respondsToSelector:@selector(contentTableView:cellForRowAtIndexPath:withColumn:)]) {
        cell.contentLabel.text = [self.excelView.dataSource contentTableView:self cellForRowAtIndexPath:contentIndexPath withColumn:indexPath.row];
    } else {
        cell.contentLabel.text = [NSString stringWithFormat:@"%ld_%ld_%ld", (long)contentIndexPath.section, (long)contentIndexPath.row, (long)indexPath.row];
    }
    //content cell 的选中状态 必须在这个地方进行设置 否者会出问题 状态会被 collectionView遮挡掉
//    SISSExcelContentTableViewCell *cellll = [self cellForRowAtIndexPath:contentIndexPath];
//    if (!cellll.selected) {
//        if (_seletedPath && contentIndexPath.row == _seletedPath.row && contentIndexPath.section == _seletedPath.section) {
//            cellll.selected = YES;
//            //            cellll.selectedBackgroundView = self.seletedView;
//        }
//        else {
//            cellll.selected = NO;
//            cellll.selectedBackgroundView = nil; //这里 一定要置空  因为 关闭了 、、、UITableViewCellSelectionStyleNone
//        }
//    }
    return cell;
}
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
    
    self.seletedPath = [self getIndexPathWith:collectionView];
    
    if ([self.excelView.delegate respondsToSelector:@selector(contentTableView:didSelectRowAtIndexPath:withColumn:)]) {
        [self.excelView.delegate contentTableView:self didSelectRowAtIndexPath:_seletedPath withColumn:indexPath.item];
    }
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath {
    NSIndexPath *contentIndexPath = [self getIndexPathWith:collectionView];
    CGFloat height = self.excelView.contentHeight;
    CGFloat width = self.excelView.contentWidth;
    if ([self.excelView.dataSource respondsToSelector:@selector(heightOfContentTableView:cellForRowAtIndexPath:)]) {
        height = [self.excelView.dataSource heightOfContentTableView:self cellForRowAtIndexPath:contentIndexPath];
    }
    if ([self.excelView.dataSource respondsToSelector:@selector(widthOfContentTableView:cellForRowAtIndexPath:)]) {
        width = [self.excelView.dataSource widthOfContentTableView:self cellForRowAtIndexPath:indexPath];
    }
    
    return CGSizeMake(width, height - 1);
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (UITableViewCell *)contentTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //行 区  indexPath
    SISSExcelContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier_Excel forIndexPath:indexPath];
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [cell.contentCollectionView registerClass:[SISSExcelContentCollectionViewCell class] forCellWithReuseIdentifier:collectionViewCellIdentifier_Excel];
    cell.contentCollectionView.tag = indexPath.row;
    cell.contentCollectionView.delegate = self;
    cell.contentCollectionView.dataSource = self;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1.0f;
    layout.minimumInteritemSpacing = 1.0f;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    cell.contentCollectionView.collectionViewLayout = layout;
    cell.contentCollectionView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    cell.contentCollectionView.bounces = NO;
    [cell.contentCollectionView reloadData];
    
    return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.excelView.dataSource respondsToSelector:@selector(contentTableView:numberOfRowsInSection:)]) {
        return [self.excelView.dataSource contentTableView:self numberOfRowsInSection:section];
    }
    return 100;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    return [self contentTableView:tableView cellForRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    if ([self.excelView.dataSource respondsToSelector:@selector(heightOfContentTableView:cellForRowAtIndexPath:)]) {
        return [self.excelView.dataSource heightOfContentTableView:self cellForRowAtIndexPath:indexPath];
    }
    return self.excelView.contentHeight;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

- (void)setTopOffset:(CGPoint)topOffset {
    _topOffset = topOffset;
    for (SISSExcelContentTableViewCell *cell in self.visibleCells) {
        cell.contentCollectionView.contentOffset = _topOffset;
    }
}
- (void)setSeletedPath:(NSIndexPath*)seletedPath {
    //    if (_seletedPath) {
    //        [self reloadRowsAtIndexPaths:@[_seletedPath] withRowAnimation:UITableViewRowAnimationNone];
    //    }
    _seletedPath = seletedPath;
    if (!seletedPath) {
        return;
    }
//    SISSExcelContentTableViewCell *contentTableViewCell = [self cellForRowAtIndexPath:_seletedPath];
//    for (SISSExcelContentTableViewCell *cell in self.visibleCells) {
//        cell.selected = NO;
//        cell.selectedBackgroundView = nil;
//    }
//    contentTableViewCell.selected = YES;
    
    
    //三点 1、在设置自定义的 选中view 之前 将cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //2、cell.selected = NO;    .selected = YES;    这两个属性必须一起进行设置
    //3、cell.selectedBackgroundView = nil;   .selectedBackgroundView = self.seletedView;    这两个属性必须一起进行设置
    
//    if ([self.excelView.delegate respondsToSelector:@selector(contentTableView:didSelectRowAtIndexPath:)]) {
//        [self.excelView.delegate contentTableView:self didSelectRowAtIndexPath:_seletedPath];
//    }
}
- (NSIndexPath *)getIndexPathWith:(UICollectionView *)collectionView {
    UIView *view = collectionView;
    while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
        view = [view superview];
    }
    UITableViewCell *cell = (UITableViewCell *)view;
    NSIndexPath *indexPath = [self indexPathForRowAtPoint:cell.center];
    return indexPath;
}


@end
